using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
public class ExportToDispatcher
{
    public Dictionary<Type, List<IParseObjectHandle>> ParseActionMap = new Dictionary<Type, List<IParseObjectHandle>>();
    private static ExportToDispatcher instance;

    public static ExportToDispatcher Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new ExportToDispatcher();
            }
            return instance;
        }
    }

    public ExportToDispatcher()
    {
        var types = typeof(ExportToDispatcher).Assembly.GetTypes();
        foreach (var type in types)
        {
            if (type.IsDefined(typeof(ParseAttribute), true) && !type.IsAbstract)
            {
                IParseObjectHandle obj = Activator.CreateInstance(type) as IParseObjectHandle;
                Type objtype = obj.Type();
                if (!ParseActionMap.ContainsKey(objtype))
                {
                    ParseActionMap.Add(objtype, new List<IParseObjectHandle>());
                }
                ParseActionMap[objtype].Add(obj);
            }
        }
    }
    public void ParseObjectToCocos(object obj, ExportSetting ExportSetting, JArray jArray)
    {
        if (ParseActionMap.TryGetValue(obj.GetType(), out List<IParseObjectHandle> list))
        {
            foreach (var handle in list)
            {
                handle.Run(ExportSetting, obj, jArray);
            }
        }
    }
}
public class ParseAttribute : Attribute
{

}
public interface IParseObjectHandle
{
    Type Type();
    void Run(ExportSetting ExportSetting, object obj, JArray jArray);
}
[Parse]
public abstract class ParseObjectHandle<T> : IParseObjectHandle
{
    public Type Type()
    {
        return typeof(T);
    }
    public abstract void Prase(ExportSetting ExportSetting, T obj, JArray jArray);

    public void Run(ExportSetting ExportSetting, object obj, JArray jArray)
    {
        this.Prase(ExportSetting, (T)obj, jArray);
    }
}
